window.addEventListener('message', function (rs) {
    console.log(rs.data);
    var hkurl = rs.data.url;
    $("#content").attr("data-url", hkurl);
});

$(document).ready(function () {
    $('#jiexi').click(function () {
        var hukeurl = $('#content').attr("data-url"); //检查网址是否是虎课网
        var str = hukeurl;
        var n = str.search(/huke88/i);
        if (n != "-1") {
            var str = hukeurl;
            var patt1 = /\/(\d.*)\.html/;
            var namey = str.match(patt1)

            if (!namey) {
                alert('请前往您要观看的页面');
                return false
            }

            var hukeID = namey[1];

            if (hukeID.indexOf("-") != -1) {
                var str = hukeID;
                var patt1 = /\d.*\-(\d.*)/;
                var namey = str.match(patt1)
                var hukeID = namey[1];
            }
            if (hukeID != "") {
                //匹配出ID

                $.post('/index/index/parse',
                    {
                        id: hukeID,
                        link:hukeurl,
                    },
                    function (data) {
                        if (data.code != 200){
                            alert('解析失败！请检查是否还未登录解析网站，或账户已经到期！')
                            $("#loading").hide();
                            return false
                        }

                        if (data.url.indexOf("huke88") != -1) {
                            $("#loading").hide();
                            $("#allbk").show();
                            bofang(data.url);//播放
                        } else {
                            alert('解析失败！请检查是否还未登录解析网站，或账户已经到期！')
                            $("#loading").hide();
                        }
                    });
            } else {
                alert('解析错误，进入想看的视频页面点击解析！');
            }
        } else {
            alert('解析错误，进入想看的视频页面点击解析！');
            return false
        }
    });


    $('#close').click(function () {
        $("#allbk").hide();
        clear();
    });

    $('#search').click(function () {
        $.post('/index/index/searchWap', {},
            function (data) {
                if (data.code == 200){
                    alert('您当前可正常使用！！过期时间:' + data.expireTime)
                }else {
                    alert('您当前不可播放视频！请前往PC端充值！！')
                }
            });
    })
});

function bofang(m3u8url) {
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
        video: {
            url: m3u8url,
            type: 'hls'
        },
    });
    dp.play();
}

function clear() {
    const dp = new DPlayer({
        container: document.getElementById('dplayer'),
    });
    dp.destroy();
}